#pragma once

//========================================================================

#include "LuaPlus/LuaPlus.h"


//---------------------------------------------------------------------------------------------------------------------
// LuaStateManager
//---------------------------------------------------------------------------------------------------------------------
class LuaStateManager : public IScriptManager
{
    static LuaStateManager* s_pSingleton;
    LuaPlus::LuaState* mp_LuaState;
    std::string m_lastError;

public:
    // Singleton functions
    static bool Create();
    static void Destroy();
    static LuaStateManager* Get() { ASSERT(s_pSingleton); return s_pSingleton; }

    // IScriptManager interface
	virtual bool VInit() override;
    virtual void VExecuteFile(const char* resource) override;
    virtual void VExecuteString(const char* str) override;

    LuaPlus::LuaObject GetGlobalVars();
    LuaPlus::LuaState* GetLuaState() const;

    // public helpers
    LuaPlus::LuaObject CreatePath(const char* pathString, bool toIgnoreLastElement = false);
    void ConvertVec3ToTable(const Vector3& vec, LuaPlus::LuaObject& outLuaTable) const;
    void ConvertTableToVec3(const LuaPlus::LuaObject& luaTable, Vector3& outVec3) const;

private:
    void SetError(int errorNum);
    void ClearStack();

    // private constructor & destructor; call the static Create() and Destroy() functions instead
    explicit LuaStateManager();
    virtual ~LuaStateManager();
};